﻿using UnityEngine;
using System.Collections;
using LitJson;
using VR_AR;

public class JsonDataRead : MonoSingleton<JsonDataRead> {

	public string ControllerBaseURL = "http://101.201.148.166/VR_AR/index.php/Admin/";
	public string DataPath		    = "http://101.201.148.166/VR_AR/uploads/";

	public Cubemap 			splashCubemap;
	public START_INFO[] 	startItems;
	public VR_INFO[]		vrHighItems;
	public VR_INFO[]		vrData;
	public AR_INFO[]		arData;
	public EVENT_INFO[]		eventData;
	public bool				bLoadFinished;
	public int 				curHighVRID;
	public int				curVRID;
	public int				curARID;
	public int 				cur3DID;
	public int				curEventID;
	public WWW				mainWWW;
	public bool				b3DVR;
	public ArrayList		textureArray;
	public ArrayList		cubemapArray;
	public ArrayList		fbxArray;
	public int[]			highItemArray;

	public IEnumerator LoadSplashData()
	{
		WWW www = new WWW(ControllerBaseURL + "GetSplashData");
		yield return www;

		JsonData json = JsonMapper.ToObject (www.text)[0];
		string url;
#if UNITY_ANDROID
		url = "android";
#endif

#if UNITY_IPHONE
		url = "ios";
#endif
		url = DataPath + json [url].ToString ();
		mainWWW = new WWW (url);
		yield return mainWWW;

		splashCubemap = (Cubemap)Instantiate(mainWWW.assetBundle.mainAsset);
		bLoadFinished = true;
		mainWWW.assetBundle.Unload (true);
	}

	public IEnumerator LoadStartItemData()
	{
		WWW www = new WWW (ControllerBaseURL + "GetStartPageData");
		yield return www;

		JsonData json = JsonMapper.ToObject (www.text) [0];
		int nCount = json.Count;
		startItems = new START_INFO[nCount];

		for (int i = 0; i < nCount; i++) {
			JsonData item = json[i];
			startItems[i].nType = int.Parse(item["type"].ToString());
			startItems[i].sImageFilepath = item["imageFilePath"].ToString();
			startItems[i].sContent = item["itemID"].ToString();
			startItems[i].sTitle = item["title"].ToString();
		}
	}

	public IEnumerator LoadHighVRData()
	{
		WWW www = new WWW (ControllerBaseURL + "GetAuto");
		yield return www;

		www = new WWW (ControllerBaseURL + "GetVRData/" + www.text);
		yield return www;
		
		JsonData json = JsonMapper.ToObject (www.text) [0];
		int nCount = json.Count;
		vrHighItems = new VR_INFO[nCount];
		
		for (int i = 0; i < nCount; i++) {
			JsonData item = json[i];
			vrHighItems[i].nID = int.Parse(item["Id"].ToString());
			vrHighItems[i].sName = item["name"].ToString();
			vrHighItems[i].sPlace = item["place"].ToString();
			vrHighItems[i].sPrice = item["price"].ToString();
			vrHighItems[i].sPhoneNumber = item["phoneNumber"].ToString();
			vrHighItems[i].sDescription = item["description"].ToString();
			vrHighItems[i].sTime = "旺季: " +item["time1"].ToString() + "    淡季: " + item["time2"].ToString();
			vrHighItems[i].sImageFilePath = item["imageFilePath"].ToString();
			vrHighItems[i].sSoundFilePath = item["soundFilePath"].ToString();
			vrHighItems[i].sTitle = item["title"].ToString();
			vrHighItems[i].nViewCount = int.Parse(item["viewCount"].ToString());
			vrHighItems[i].nLikeCount = int.Parse(item["likeCount"].ToString());

			www = new WWW(ControllerBaseURL + "GetVRImageData/" + vrHighItems[i].nID);
			yield return www;

			JsonData jsonImage = JsonMapper.ToObject(www.text)[0];
			int nImageCount = jsonImage.Count;
			vrHighItems[i].vrImages = new VRIMAGE_INFO[nImageCount];

			for(int j = 0; j < nImageCount; j++) {
				JsonData itemImage = jsonImage[j];
				vrHighItems[i].vrImages[j].introImageFilePath = itemImage["introImageFilePath"].ToString();
#if UNITY_ANDROID
				vrHighItems[i].vrImages[j].assetFilePath = itemImage["androidFilePath"].ToString();
#elif UNITY_IPHONE
				vrHighItems[i].vrImages[j].assetFilePath = itemImage["iosFilePath"].ToString();
#endif
			}

			www = new WWW(ControllerBaseURL + "GetVR3DData/" + vrHighItems[i].nID);
			yield return www;

			JsonData json3D = JsonMapper.ToObject(www.text)[0];
			int n3DCount = json3D.Count;
			vrHighItems[i].vr3DItems = new VR3D_INFO[n3DCount];

			for(int j = 0; j < n3DCount; j++) {
				JsonData item3D = json3D[j];
				vrHighItems[i].vr3DItems[j].nID = int.Parse(item3D["Id"].ToString());
				vrHighItems[i].vr3DItems[j].sName = item3D["name"].ToString();
				vrHighItems[i].vr3DItems[j].sDescription = item3D["description"].ToString();
				vrHighItems[i].vr3DItems[j].sMainImageFilePath = item3D["mainImageFilePath"].ToString();
				vrHighItems[i].vr3DItems[j].sMenuImageFilePath = item3D["menuImageFilePath"].ToString();
				vrHighItems[i].vr3DItems[j].sSoundFilePath = item3D["soundFilePath"].ToString();
#if UNITY_ANDROID
				vrHighItems[i].vr3DItems[j].sAssetBundlePath = item3D["androidFilePath"].ToString();
#elif UNITY_IPHONE
				vrHighItems[i].vr3DItems[j].sAssetBundlePath = item3D["iosFilePath"].ToString();
#endif
			}
		}

		StartCoroutine (GetHighVRInfo ());
	}

	public IEnumerator LoadVRData()
	{
		WWW www = new WWW (ControllerBaseURL + "GetVRData");
		yield return www;
		
		JsonData json = JsonMapper.ToObject (www.text) [0];
		int nCount = json.Count;
		vrData = new VR_INFO[nCount];
		
		for (int i = 0; i < nCount; i++) {
			JsonData item = json[i];
			vrData[i].nID = int.Parse(item["Id"].ToString());
			vrData[i].sImageFilePath = item["imageFilePath"].ToString();
			vrData[i].sTitle = item["title"].ToString();

			www = new WWW(ControllerBaseURL + "GetVRImageData/" + vrData[i].nID);
			yield return www;

			JsonData jsonImage = JsonMapper.ToObject(www.text)[0];
			int nImageCount = jsonImage.Count;
			vrData[i].vrImages = new VRIMAGE_INFO[nImageCount];
			
			for(int j = 0; j < nImageCount; j++) {
				JsonData itemImage = jsonImage[j];
				vrData[i].vrImages[j].introImageFilePath = itemImage["introImageFilePath"].ToString();
				vrData[i].vrImages[j].imageName = itemImage["imageName"].ToString();
#if UNITY_ANDROID
				vrData[i].vrImages[j].assetFilePath = itemImage["androidFilePath"].ToString();
#elif UNITY_IPHONE
				vrData[i].vrImages[j].assetFilePath = itemImage["iosFilePath"].ToString();
#endif
			}

			www = new WWW(ControllerBaseURL + "GetVR3DData/" + vrData[i].nID);
			yield return www;

			JsonData json3D = JsonMapper.ToObject(www.text)[0];
			int n3DCount = json3D.Count;
			vrData[i].vr3DItems = new VR3D_INFO[n3DCount];
			
			for(int j = 0; j < n3DCount; j++) {
				JsonData item3D = json3D[j];
				vrData[i].vr3DItems[j].nID = int.Parse(item3D["Id"].ToString());
				vrData[i].vr3DItems[j].sName = item3D["name"].ToString();
				vrData[i].vr3DItems[j].sDescription = item3D["description"].ToString();
				vrData[i].vr3DItems[j].sMainImageFilePath = item3D["mainImageFilePath"].ToString();
				vrData[i].vr3DItems[j].sMenuImageFilePath = item3D["menuImageFilePath"].ToString();
				vrData[i].vr3DItems[j].sSoundFilePath = item3D["soundFilePath"].ToString();
#if UNITY_ANDROID
				vrData[i].vr3DItems[j].sAssetBundlePath = item3D["androidFilePath"].ToString();
				vrData[i].vr3DItems[j].sVRImagePath = item3D["androidVRImagePath"].ToString();
#elif UNITY_IPHONE
				vrData[i].vr3DItems[j].sAssetBundlePath = item3D["iosFilePath"].ToString();
				vrData[i].vr3DItems[j].sVRImagePath = item3D["iosVRImagePath"].ToString();
#endif
			}
		}
	}

	public IEnumerator LoadEventData()
	{
		WWW www = new WWW (ControllerBaseURL + "GetItemData");
		yield return www;
		
		JsonData json = JsonMapper.ToObject (www.text) [0];
		int nCount = json.Count;
		eventData = new EVENT_INFO[nCount];
		
		for (int i = 0; i < nCount; i++) {
			JsonData item = json[i];
			eventData[i].nID = int.Parse(item["Id"].ToString());
			eventData[i].sTitle = item["title"].ToString();
			eventData[i].sDate = item["date"].ToString();

			www = new WWW(ControllerBaseURL + "GetEventData/" + eventData[i].nID);
			yield return www;

			JsonData jsonImage = JsonMapper.ToObject(www.text)[0];
			int nImageCount = jsonImage.Count;
			eventData[i].eventImages = new IMAGE_INFO[nImageCount];

			for(int j = 0; j < nImageCount; j++) {
				JsonData itemImage = jsonImage[j];
				eventData[i].eventImages[j].sImageFilePath = itemImage["imageFilePath"].ToString();
				eventData[i].eventImages[j].sDescription = itemImage["description"].ToString();
			}
		}
	}

	public IEnumerator LoadARData()
	{
		WWW www = new WWW (ControllerBaseURL + "GetARData");
		yield return www;
		
		JsonData json = JsonMapper.ToObject (www.text) [0];
		int nCount = json.Count;
		arData = new AR_INFO[nCount];
		
		for (int i = 0; i < nCount; i++) {
			JsonData item = json[i];
			arData[i].nID = int.Parse(item["Id"].ToString());
			arData[i].sMenuImageFilepath = item["imageFilePath"].ToString();
			arData[i].sName = item["name"].ToString();
			
			www = new WWW(ControllerBaseURL + "GetARImageData/" + arData[i].nID);
			yield return www;
			
			JsonData jsonImage = JsonMapper.ToObject(www.text)[0];
			int nImageCount = jsonImage.Count;
			arData[i].arImages = new IMAGE_INFO[nImageCount];

			for(int j = 0; j < nImageCount; j++) {
				JsonData itemImage = jsonImage[j];
				arData[i].arImages[j].sImageFilePath = itemImage["imageFilePath"].ToString();
				arData[i].arImages[j].sDescription = itemImage["description"].ToString();
			}
		}
	}

	public void FindHighVRIndex(int vrID)
	{
		for (int i = 0; i < vrHighItems.Length; i++)
			if (vrHighItems[i].nID == vrID) {
				curHighVRID = i;
				break;
			}
	}

	public void FindVRIndex(int vrID)
	{
		for (int i = 0; i < vrData.Length; i++)
			if (vrData[i].nID == vrID) {
				curVRID = i;
				break;
			}
	}

	public void FindARIndex(int arID)
	{
		for (int i = 0; i < arData.Length; i++)
			if (arData[i].nID == arID) {
				curARID = i;
				break;
			}
	}

	public IEnumerator IncViewCount()
	{
		WWW www = new WWW (ControllerBaseURL + "IncViewCount/" + vrHighItems[curHighVRID].nID.ToString());
		yield return www;
		vrHighItems [curHighVRID].nViewCount = int.Parse(www.text);
	}
	
	public IEnumerator IncLikeCount()
	{
		WWW www = new WWW (ControllerBaseURL + "IncLikeCount/" + vrHighItems[curHighVRID].nID.ToString());
		yield return www;
		vrHighItems [curHighVRID].nLikeCount = int.Parse(www.text);
	}

	public Texture FindTextureByUrl(string url)
	{
		foreach (STexture sTex in textureArray)
			if (sTex.url == url)
				return sTex.texture;

		return null;
	}

	public Cubemap FindCubemapByUrl(string url)
	{
		foreach (SCubeMap sCube in cubemapArray)
			if (sCube.url == url)
				return sCube.cubemap;
		
		return null;
	}

	public GameObject FindFbxByUrl(string url)
	{
		foreach (SFbx sFbx in fbxArray)
			if (sFbx.url == url)
				return sFbx.fbx;
		
		return null;
	}

	public void AddTexture(string url, Texture tex)
	{
		STexture sTex = new STexture ();
		sTex.url = url;
		sTex.texture = tex;
		textureArray.Add (sTex);
	}

	public IEnumerator GetHighVRInfo()
	{
		WWW www = new WWW (ControllerBaseURL + "GetAuto");
		yield return www;
		
		www = new WWW (ControllerBaseURL + "GetVRData/" + www.text);
		yield return www;
		
		JsonData json = JsonMapper.ToObject (www.text) [0];
		int nCount = json.Count;
		highItemArray = new int[nCount];
		
		for (int i = 0; i < nCount; i++) {
			JsonData item = json [i];
			int nID = int.Parse (item ["Id"].ToString ());
			for(int j = 0; j < nCount; j++) {
				if(vrHighItems[j].nID == nID) {
					highItemArray[i] = j;
					break;
				}
			}
		}
	}
}